var builder = DistributedApplication.CreateBuilder(args);
const string scheme = "http";
//宿主机地址，因为在容器中访问localhost无法访问宿主机，所以使用host.docker.internal代替，
var hostEndpoint = $"{scheme}://host.docker.internal";
string identityGatewayEndpoint = $"{hostEndpoint}/api/identity";
string basicGatewayEndpoint = $"{hostEndpoint}/api/basic";
//consul地址
var consulEndpoint = $"{hostEndpoint}:8500";
var identityEndpoint = $"{hostEndpoint}:18000";


var consul = builder.AddContainer("consul", "consul", "latest")
    .WithEndpoint(port: 8500, targetPort: 8500, name: scheme, scheme: scheme)    // 关键：添加 Consul 启动命令，指定固定节点名
    .WithArgs(
        "agent",
        "-server",
        "-ui",
        "-bootstrap",
        "-client=0.0.0.0",
        "-datacenter=dc1",
        "-node=host.docker.internal"  // 固定节点名，避免使用容器 ID
    );

var cache = builder.AddRedis("cache")
                   .WithImageTag("latest")
                   .WithLifetime(ContainerLifetime.Persistent)
                   .WithRedisInsight(optons =>
                   {
                       optons.WithHostPort(8551);
                       optons.WithImageTag("latest");
                   });

var password = builder.AddParameter("mysql-password", secret: true);
var mysql = builder.AddMySql("erp-mysql", password, 3308)
                   .WithLifetime(ContainerLifetime.Persistent)
                   .WithImageTag("latest")
                   .WithDataBindMount(source: @"C:\Docker\erp-mysql")
                   .WithPhpMyAdmin(optons =>
                   {
                       optons.WithHostPort(8550);
                       optons.WithImageTag("latest");
                   });

var identityserviceDb = mysql.AddDatabase("identityserviceDb");
var productDb = mysql.AddDatabase("productDb");

var identity = builder
    .AddProject<Projects.IdentityApi>("identity",
        configure: static project =>
        {
            project.ExcludeLaunchProfile = false;
            project.ExcludeKestrelEndpoints = false;
        })
    .WithEndpoint(port: 8000, targetPort: 18000, name: scheme, scheme: scheme)
    .WaitFor(consul)
    .WithReference(cache)
    .WithReference(identityserviceDb)
    .WaitFor(identityserviceDb)
    .WithEnvironment("ConsulOptions__Address", consulEndpoint)
    // 添加以下行设置环境变量
    //.WithEnvironment("OTEL_EXPORTER_OTLP_ENDPOINT", "True")

    ;

var basic = builder.AddProject<Projects.BasicApi>("basic",
        configure: static project =>
        {
            project.ExcludeLaunchProfile = false;
            project.ExcludeKestrelEndpoints = false;
        })
    .WithEndpoint(port: 8002, targetPort: 18002, name: scheme, scheme: scheme)
    .WaitFor(consul)
    .WithReference(cache)
    .WithReference(productDb)
    .WaitFor(productDb)
    .WithEnvironment("ConsulOptions__Address", consulEndpoint)
    // 添加以下行设置环境变量
    //.WithEnvironment("OTEL_EXPORTER_OTLP_ENDPOINT", "True")
    ;


var gateway = builder.AddProject<Projects.ELF_Gateway>("gateway",
        configure: static project =>
        {
            project.ExcludeLaunchProfile = false;
            project.ExcludeKestrelEndpoints = false;
        })
    .WithEndpoint(port: 8100, targetPort: 18100, name: scheme, scheme: scheme)
    .WithReference(cache)
    .WaitFor(consul)
    .WaitFor(cache)
    .WithEnvironment("ConsulOptions__Address", consulEndpoint)
    .WithEnvironment("OpenIddict__Issuer", identityEndpoint)

    // 添加以下行设置环境变量
    //.WithEnvironment("OTEL_EXPORTER_OTLP_ENDPOINT", "True")
    ;

//var gateway = builder.AddProject<Projects.Gate>("elf-gateway",
//        configure: static project =>
//        {
//            project.ExcludeLaunchProfile = false;
//            project.ExcludeKestrelEndpoints = false;
//        })
//    .WithHttpEndpoint(port: 8100, targetPort: 18100, name: scheme)
//                       .WithReference(identityService)
//    .WithEnvironment("OpenIddict__Issuer", identityEndpoint)
//    // 添加以下行设置环境变量
//    //.WithEnvironment("OTEL_EXPORTER_OTLP_ENDPOINT", "True")
//    ;
//builder.AddProject<Projects.Gate>("gate");
//builder.AddProject<Projects.Gate>("gate");


//var gateway = builder.AddProject<Projects.Gate>("elf-gateway",
//        configure: static project =>
//        {
//            project.ExcludeLaunchProfile = false;
//            project.ExcludeKestrelEndpoints = false;
//        })
//    .WithHttpEndpoint(port: 8100, targetPort: 18100, name: scheme)
//                       .WithReference(identityService)
//    .WithEnvironment("OpenIddict__Issuer", identityEndpoint)
//    // 添加以下行设置环境变量
//    //.WithEnvironment("OTEL_EXPORTER_OTLP_ENDPOINT", "True")
//    ;
//builder.AddProject<Projects.Gate>("gate");
//builder.AddProject<Projects.Gate>("gate");


builder.Build().Run();
